package cn.xiaobai.controller;

import cn.xiaobai.domain.User;
import cn.xiaobai.service.UserServer;
import cn.xiaobai.utils.JWTUtils;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.IncorrectClaimException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yangdaji
 * @Date 2024/2/23
 * @PackageName cn.xiaobai.controller
 * @ClassName LoginController
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserServer userServer;

    /**
     * 用户登录
     *
     * @param user 用户认证信息
     * @return
     */
    @GetMapping("/login")
    public ResponseEntity<Map> login(User user) {
        // 打印用户登录信息, 实际中一般不会打印密码,并且会对密码进行加密
        log.info("用户名: {}", user.getUsername());
        log.info("密码: {}", user.getPassword());

        // 这里将返回信息使用 map 返回, 不必设置统一返回类
        Map<String, Object> response = new HashMap<>();
        try {
            Map<String, String> payload = new HashMap<>();
            // 处理登录逻辑
            User logined = userServer.login(user);

            payload.put("username", logined.getUsername());
            // 生成 token
            String token = JWTUtils.createToken(payload);

            response.put("state", true);
            response.put("msg", "登录成功!");
            response.put("token", token);
        } catch (NotFoundException e) {
            response.put("state", false);
            response.put("msg", "登录失败: " + e.getMessage());
        }
        // 获取用户信息, 设置为 map 并生成 token
        return ResponseEntity.ofNullable(response);
    }


    /**
     * 通过 token 获取资源数据
     *
     * @param token 认证token
     * @return
     */
    @PostMapping("/data")
//    public ResponseEntity<Map> queryData(String token) {
//
//        Map<String, Object> response = new HashMap<>();
//        try {
//            // token 验证
//            JWTUtils.verifyToken(token);
//            response.put("state", true);
//            response.put("msg", "请求成功");
//            response.put("data", "获取的数据");
//            return ResponseEntity.ofNullable(response);
//        } catch (SignatureVerificationException e) {
//            log.error("签名无线: {}", e.getMessage());
//            response.put("msg", "签名无效");
//        } catch (TokenExpiredException e2) {
//            log.error("token 过期: {}", e2.getMessage());
//            response.put("msg", "token 过期");
//            throw new RuntimeException(e2);
//        } catch (AlgorithmMismatchException | IncorrectClaimException e3) {
//            log.error("token 无效: {}", e3.getMessage());
//            response.put("msg", "token 无效");
//        } catch (Exception e4) {
//            log.error("签名无线: {}", e4.getMessage());
//            response.put("msg", "签名无效");
//        }
//        response.put("state", false);
//        return ResponseEntity.ofNullable(response);
//    }

    public ResponseEntity<Map> queryData(HttpServletRequest request) {

        Map<String, Object> response = new HashMap<>();
        String token = request.getHeader("token");
        // token 验证
        JWTUtils.verifyToken(token);
        response.put("state", true);
        response.put("msg", "请求成功");
        response.put("data", "获取的数据");
        return ResponseEntity.ofNullable(response);
    }
}
